package com.web.dao.impl;

import com.util.LogUtil;
import com.web.dao.CityDao;
import com.web.domain.City;
import com.web.domain.mapper.CityMapper;
import com.web.vo.mapper.StringMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈国钰 on 2016/9/5.
 */
@Repository
public class CityDaoImpl implements CityDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public List<City> searchCity() {
        String sql = "select *  from t_city";
        return jdbcTemplate.query(sql,new CityMapper());
    }

    public List<String> searchLandmarks(String cityName) {
        String sql =
                "SELECT\n" +
                        "	DISTINCT(landmarks)\n" +
                        "FROM\n" +
                        "	t_city\n" +
                        "WHERE\n" +
                        "	cityName = ?";
        List<String> param = new ArrayList<String>();
        param.add(cityName);
        System.out.println(sql);
        return jdbcTemplate.query(sql, param.toArray(), new StringMapper());
    }

    public List<City> searchAreaName(String cityName, String landmarks) {
        String sql =
                "SELECT\n" +
                        "	*\n" +
                        "FROM\n" +
                        "	t_city\n" +
                        "WHERE\n" +
                        "	cityName = ?\n" +
                        "AND landmarks = ?";
        LogUtil.sqlLog("根据城市名称与主要地标获得城市ID",sql);
        List<String> params = new ArrayList<String>();
        params.add(cityName);
        params.add(landmarks);
        return jdbcTemplate.query(sql,params.toArray(),new CityMapper());
    }
}
